package com.itheima.controller;

import com.itheima.domain.PageResult;
import com.itheima.domain.Result;
import com.itheima.domain.pojo.Clazz;
import com.itheima.domain.pojo.ClazzQueruparam;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {
    @Autowired
    private ClazzService clazzService;
    //新增班级
    @PostMapping
    public Result saveClazz(@RequestBody Clazz clazz) {
        clazzService.save(clazz);
        return Result.success();
    }
    //查询所有班级
    @GetMapping("/list")
    public Result saveList(){
        List<Clazz> clazzs = clazzService.saveList();
        return Result.success(clazzs);
    }
    //班级列表查询
    @GetMapping
    public Result page(ClazzQueruparam clazzQueruparam) {
        log.info("分页查询：clazzQueruparam {}", clazzQueruparam);
        PageResult<Clazz> PageResult = clazzService.page(clazzQueruparam);
        return Result.success(PageResult);
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        System.out.println("根据id删除班级，id="+ id);
        clazzService.deleteById(id);
        return Result.success();
    }
    //根据id查询班级
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }
    //根据id修改班级
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("修改班级信息：{}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }

}
